package cl.utalca.robot.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cl.utalca.DB.DBBase;
import cl.utalca.robot.dto.LanguageDTO;
import cl.utalca.robot.interfaces.LanguageInterface;


public class LanguageDAO extends DBBase implements LanguageInterface{

	public LanguageDAO (Connection conn){
		
		this.conn = conn;
	}
	
	public List<LanguageDTO> getLanguages() {
		
		List<LanguageDTO> languageList = new ArrayList<LanguageDTO>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		String query = "SELECT * FROM lenguaje";
		
		LanguageDTO language;
		
		try {
			pstmt = conn.prepareStatement(query);
			rs = pstmt.executeQuery();
			
			while (rs.next()){
				
				language = new LanguageDTO();
				language.setId(rs.getInt("id_lenguaje"));
				language.setLanguageName(rs.getString("nombre"));
				language.setCompileCommand(rs.getString("compilador"));
				language.setExecuteCommand(rs.getString("ejecucion"));
				
				languageList.add(language);
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
		return languageList;
	}
	
	public int saveLanguage(LanguageDTO language){
		
		PreparedStatement pstmt = null;
		int resp = -1;
		String query = " INSERT INTO lenguaje(nombre, compilador, ejecucion)" +
					   " VALUES (?, ?, ?);";
		
		try{
		
			pstmt = conn.prepareStatement(query);
			pstmt.setString(1, language.getLanguageName());
			pstmt.setString(2, language.getCompileCommand());
			pstmt.setString(3, language.getExecuteCommand());
			resp = pstmt.executeUpdate();
			
		}
		catch(SQLException sqle){
			sqle.printStackTrace();			
		}			
		finally{
			try {
				pstmt.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}
		}
		return resp;
	}
	
	public int deleteLanguage(int languageId){
		
		PreparedStatement pstmt = null;
		int resp = -1; 
		String query = " DELETE FROM lenguaje WHERE id_lenguaje = ?;";
		
		try{
			
			pstmt = conn.prepareStatement(query);
			pstmt.setInt(1, languageId);
			
			resp = pstmt.executeUpdate();
			
			
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally{			
			try {
				pstmt.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}
		}
		return resp;
	}

	
	public LanguageDTO getLanguage(int LanguageId) {
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		LanguageDTO resp = null;

		String query = "SELECT nombre, compilador, ejecucion, id_lenguaje FROM lenguaje" +
				" WHERE id_lenguaje = ?;";
		try {
			pstmt = conn.prepareStatement(query);
			pstmt.setInt(1, LanguageId);
			
			rs = pstmt.executeQuery();
			
			if (rs.next()){
				resp = new LanguageDTO();
				
				resp.setId(rs.getInt("id_lenguaje"));
				resp.setLanguageName(rs.getString("nombre"));
				resp.setCompileCommand(rs.getString("compilador"));
				resp.setExecuteCommand(rs.getString("ejecucion"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				pstmt.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
					
		return resp;
	}

	@Override
	public int updateLanguage(LanguageDTO language) {
		
		PreparedStatement pstmt = null;
		int resp = -1;
		String query = " UPDATE lenguaje SET nombre = ?, compilador = ?, ejecucion = ? WHERE id_lenguaje = ?";
					   
		
		try{
		
			pstmt = conn.prepareStatement(query);
			pstmt.setString(1, language.getLanguageName());
			pstmt.setString(2, language.getCompileCommand());
			pstmt.setString(3, language.getExecuteCommand());
			pstmt.setInt(4, language.getId());
			resp = pstmt.executeUpdate();
			
		}
		catch(SQLException sqle){
			sqle.printStackTrace();			
		}			
		finally{
			try {
				pstmt.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}
		}
		return resp;
	}
}
